<?php
/**
 *Controla la administracion de ciudades
 *@author Luis Gabriel Rodriguez <luisgabriel84@gmail.com>
 */

/**
 *Controlador para la administracion de ciudades
 */
class CitiesController extends AppController {

	var $name = 'Cities';
        /**
        * @var array
        * @access public
        */
	var $helpers = array('Html', 'Form','Javascript','Ajax');
        
        /**
        * @var array
        * @access public
        */
	var $components = array('RequestHandler');

	
        /**
         *
         * Listado de ciudades en el admin
         */

	function admin_index() {
	    $this->layout='admin';
		$this->City->recursive = 0;
		$this->set('cities', $this->paginate());
	}

        /**
         *
         * Agregar una nueva ciudad
         */
	function admin_add() {
	    $this->layout='admin';
		if (!empty($this->data)) {
			$this->City->create();
			if ($this->City->save($this->data)) {
				$this->Session->setFlash(__('The City has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The City could not be saved. Please, try again.', true));
			}
		}
		$states = $this->City->State->find('list');
		$this->set(compact('states'));
	}
	
        /**
         *
         * Modificar una ciudad existente
         * @param integer $id identificador de la ciudad
         */
	function admin_edit($id = null) {
	$this->layout='admin';
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid City', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->City->save($this->data)) {
				$this->Session->setFlash(__('The City has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The City could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->City->read(null, $id);
		}
		$states = $this->City->State->find('list');
		$this->set(compact('states'));
	}
        /**
         *
         * Eliminar la ciudad
         * @param integer $id identificador de la ciudad
         */
	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for City', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->City->del($id)) {
			$this->Session->setFlash(__('City deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>